package com.mianyang.zhangsan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mianyang.zhangsan.common.util.CommonUtils;
import com.mianyang.zhangsan.model.dto.SecGrant;
import com.mianyang.zhangsan.model.dto.SecGrantRoleDto;
import com.mianyang.zhangsan.model.entity.SecUserRole;
import com.mianyang.zhangsan.mapper.SecUserRoleMapper;
import com.mianyang.zhangsan.service.ISecUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangjr
 * @since 2022-10-23
 */
@Service
public class SecUserRoleServiceImpl extends ServiceImpl<SecUserRoleMapper, SecUserRole> implements ISecUserRoleService {
    @Override
    public List<Long> selectRoles(Long userId) {
        QueryWrapper<SecUserRole> wrapper = new QueryWrapper<>();
        if (userId != null) {
            // 查询用户角色关系
            wrapper.eq("user_id", userId);
            return convert(list(wrapper));
        }
        return null;
    }

    @Override
    public void grantAuthorities(Long roleId, Long userId) {
        SecUserRole userRole = SecUserRole.builder()
                .userId(userId)
                .roleId(roleId).build();
        save(userRole);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void grant(SecGrant grant) {
        Long relationId = grant.getRelationId();
        if (relationId != null) {
            QueryWrapper<SecUserRole> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", relationId);
            remove(wrapper);
            List<Long> relations = grant.getRelations();
            if (CommonUtils.isNotEmpty(relations)) {
                List<SecUserRole> userRoles = new ArrayList<>();
                for (Long roleId : relations) {
                    if (roleId != null) {
                        SecUserRole userRole = SecUserRole.builder().userId(relationId).roleId(roleId).build();
                        userRoles.add(userRole);
                    }
                }
                saveBatch(userRoles);
            }
        }
    }

    @Override
    public String grantToUser(SecUserRole userRole) {
        String tip = "授权成功";
        try {
            Long userId = userRole.getUserId();
            QueryWrapper<SecUserRole> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id", userId);
            remove(wrapper);
            save(userRole);
        } catch (Exception e) {
            tip = e.getMessage();
        }
        return tip;
    }

    private List<Long> convert(List<SecUserRole> secUserRoles) {
        if (secUserRoles == null || secUserRoles.size() <= 0) {
            return null;
        }
        return secUserRoles.stream()
                .map(SecUserRole::getRoleId)
                .collect(Collectors.toList());
    }
}
